const request = require('request')
const semver = require('semver')
const chalk = require('chalk')
const ora = require('ora')
const log = console.log
const package = require('../package.json')
module.exports = callback => {
  const spinner = ora('正在加载请稍后...').start()
  spinner.color = 'yellow'
  request(
    {
      url: 'https://registry.npmjs.org/codenalyse',
      timeout: 1000,
    },
    (err, res, body) => {
      spinner.text = 'success'
      spinner.succeed()
      if (!err && res.statusCode === 200) {
        const latestVersion = JSON.parse(body)['dist-tags'].latest
        const localVersion = package.version
        if (semver.lt(localVersion, latestVersion)) {
          log()
          log(chalk.yellow('  ★ 有新版本更新 ★'))
          log()
          log('  latest:    ' + chalk.green(latestVersion))
          log('  installed: ' + chalk.red(localVersion))
          log()
        }
      }
      callback()
    }
  )
}
